/*
 *  Copyright 2009 Lucas Nazário dos Santos
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sourceforge.retriever.frontier;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * TODO Add javadoc.
 */
public class OneTimeOnlyFrontier implements Frontier {


	private final Frontier frontier;

	private final Set<String> urlsAlreadyAdded = new HashSet<String>();

	/**
	 * TODO Add javadoc.
	 */
	public OneTimeOnlyFrontier(final Frontier urlFrontier) {
		this.frontier = urlFrontier;
	}

	/**
	 * TODO Add javadoc.
	 */
	public void enqueue(final FrontierUrl frontierURL) {
		final String url = frontierURL.getUrl();
		if (!this.urlsAlreadyAdded.contains(url)) {
			this.frontier.enqueue(frontierURL);
			synchronized (this) {
				this.urlsAlreadyAdded.add(url);
			}
		}
	}

	/**
	 * TODO Add javadoc.
	 */
	public List<FrontierUrl> dequeue() {
		return this.frontier.dequeue();
	}

	/**
	 * TODO Write javadoc.
	 */
	public void notifyFetchedIp(final String ip) {
		this.frontier.notifyFetchedIp(ip);
	}

	/**
	 * TODO Add javadoc.
	 */
	public boolean hasURLs() {
		return this.frontier.hasURLs();
	}

	/**
	 * TODO Add javadoc.
	 */
	public void reset() {
		synchronized (this) {
			this.urlsAlreadyAdded.clear();
		}
		this.frontier.reset();
	}

	/**
	 * TODO Add javadoc.
	 */
	public void setIntervalBetweenFetchesInMillis(final long time) {
		this.frontier.setIntervalBetweenFetchesInMillis(time);
	}

	/**
	 * TODO Add javadoc.
	 */
	public void setIntervalBetweenFetchesInMillis(final String ip, final long time) {
		this.frontier.setIntervalBetweenFetchesInMillis(ip, time);
	}
}